<?php
class StockModel extends Model{
	public $table = 'stock';
	public $validate = array(

		);

	public function addStock($gid){
		if(isset($_POST['spec'])){
			$spec = $_POST['spec'];
		}
		$cid = $_POST['cid'];
		// 如果存在规格字段
		if(isset($spec)){	
			// 将规格中的图片插入库存表中
			$picData = K('GoodsPic')->where(array('goods_gid'=>$gid))->all();
			foreach ($spec['price'] as $key => $value) {
				$stid = $this->add(array('gid'=>$gid,'price'=>$spec['price'][$key],
					'stock'=>$spec['stock'][$key],'goods_sn'=>$spec['goods_sn'][$key],
					'stock_pic'=>$picData[$key]['pic'],
					'pic_id'=>$picData[$key]['pic_id']));

				foreach ($spec['gg'] as $n => $d) {
					$said = M('stock_attr')->add(array('cid'=>$cid,'gid'=>$gid,'stid'=>$stid,'av_id'=>$d[$key]));
				}
			}
		}else{
			// 不存在的话直接将库存、商品id、及商品pic_id插入库存表即可
			$picData = K('GoodsPic')->where(array('goods_gid'=>$gid))->all();
			$this->add(array('gid'=>$gid,'price'=>$_POST['price'],
					'stock'=>$_POST['stock'],'goods_sn'=>$_POST['goods_sn'],
					'pic_id'=>$picData[0]['pic_id']));
		}
		return true;
	}
	//整理二维数组 形成新的数组 规格 库存相对应的数组
	// public function arrChange($arr){
	// 	$newarr = array();
	// 	foreach ($arr['price'] as $key => $value) {
	// 		// $newarr[$key][]=array();
	// 		foreach ($arr as $spec => $d) {
	// 			$newarr[$key][] =  $d[$key];
	// 		}
		
	// 	}
	// 	return $newarr;
	// }
	
	
	//根据stid删除库存数据和库存属性关联表数据
	public function delStock($stid){
		//删除库存数据
		if($this->delete('stid='.$stid)){

			if($this->delStockAttr($stid)){
				return true;
			}else{
				$this->error .= '商品库存属性关联删除失败';
				return false;
			}
			
		}else{
			$this->error = '商品库存删除失败';
			return false;
		}
		
		
	}
	public function delStockAttr($stid){
		//删除库存关联表数据
		M('stock_attr')->delete('stid='.$stid);
	}


	//删除商品时删除该商品的所有库存及库存属性关联数据  根据gid删除
	public function gidStockDel($gid){
		$this->delete('gid='.$gid);
		$this->gidStockAttrDel($gid);
		return true;
	}

	public function gidStockAttrDel($gid){
		M('stock_attr')->delete('gid='.$gid);
		return true;
	}
	/**
	 * 更新库存 和库存属性表 当编辑商品时调用
	 * @return [type] [description]
	 */
	public function saveStock($gid){
		$specAttr = $_POST['spec'];
		if(!isset($specAttr)){
			return;
		}
		foreach ($specAttr['price'] as $n => $spec) {
			$data = array();
			//存在stid那么就更新，因为之前已经将 gid符合的全部删除了
			if(isset($specAttr['stid'][$n])){
				$data['stid'] = intval($specAttr['stid'][$n]);
			}else{
				$data['stid'] = null;
			}
			$data['price'] = $specAttr['price'][$n];
			$data['stock'] = intval($specAttr['stock'][$n]);
			$data['goods_sn'] = $specAttr['goods_sn'][$n];
			$data['gid'] = intval($gid);
			$data['stock_pic'] = $specAttr['stock_pic'][$n];
			$data['pic_id'] = $specAttr['pic_id'][$n];
			$stid = $this->add($data);
			if($stid){
				foreach ($specAttr['gg'] as $key => $value) {
					$addData = array();				
					$arr = explode('|',$value[$n]);
					$av_id = $arr[0];
					$addData['stid'] = $stid;
					$addData['cid'] = Q('cid');
					$addData['gid'] = Q('gid');
					$addData['av_id'] = $av_id;
					M('stock_attr')->add($addData);
				}
			}
		}
	}
}
?>
